import 'package:flutter/material.dart';
import 'package:uikit/uikit.dart';

import 'entry_item.dart';

class EntryWidget extends StatelessWidget {
  final List<EntryItem> items;
  const EntryWidget({super.key, required this.items});

  @override
  Widget build(BuildContext context) {
    var width = MediaQuery.of(context).size.width - 32;
    return Container(
      height: _viewHeight(),
      margin: const EdgeInsets.only(top: 8, bottom: 8),
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(
              Radius.circular(24)
          )
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: items.map((e) => Expanded(
          child: _buildItemWidget(e, width / items.length),
        )).toList(),
      ),
    );
  }

  Widget _buildItemWidget(EntryItem item, double width) {
    double fixWidth = width - 62;
    if(items.length == 3) {
      if(item.isNoHomeAddress()) {
        fixWidth = width - 59;
      } else if(item.isNoCompanyAddress()) {
        fixWidth = width - 48;
      }
    }

    if (item.direction == T3EntryItemAxis.vertical) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              // 竖线
              if (item.showDividerLine)
                Container(
                    margin: const EdgeInsets.only(top: 8, bottom: 8),
                    width: 0.5,
                    decoration: const BoxDecoration(color: Color(0xFFF2F2F2))
                ),
              // 图标
              Container(
                margin: const EdgeInsets.only(left: 12, right: 8),
                width: 16,
                height: 16,
                child: T3Image.t3assets(T3AssetsNameString(item.iconName)),
              ),
              // 标题和文案
              // 如果是 图选 宽度要小一点 如果是公司 宽度要大一点
              T3GestureDetector(
                onTap: () {
                  if (item.clicked != null) {
                    item.clicked!();
                  }
                },
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const SizedBox(height: 8,),
                    Container(
                      width: width - 62,
                      height: 16,
                      color: Colors.transparent,
                        child: Text(
                          item.title,
                          overflow: TextOverflow.ellipsis,
                          style: const TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.w500,
                              color: Color(0xFF02102A)),
                        ),
                      ),
                      const SizedBox(height: 4,),
                      Container(
                        width: fixWidth,
                        height: 26,
                        color: Colors.transparent,
                        child: Text(
                          item.subtitle,
                          overflow: TextOverflow.ellipsis,
                          style: const TextStyle(fontSize: 11, color: Color(0xFFB7BECC)),
                        ),
                      ),
                  ],
                ),
              ),
            ],
          ),
          // 三个点
          if (item.detailIconName != null)
            T3GestureDetector(
              onTap: () {
                if (item.clickedDetail != null) {
                  item.clickedDetail!();
                }
              },
              child: Container(
                margin: const EdgeInsets.only(right: 6),
                width: 20,
                height: 40,
                color: Colors.transparent,
                child: T3Image.t3assets(T3AssetsNameString(item.detailIconName!)),
              ),
            ),
        ],
      );
    } else {
      List<Widget> list = [
        T3Image.t3assets(T3AssetsNameString(item.iconName)),
        Text(
          item.title,
          style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w500,
              color: Color(0xFF02102A)),
        ),
        Text(
          item.subtitle,
          style: const TextStyle(fontSize: 11, color: Color(0xFFB7BECC)),
        ),
      ];
      if (item.detailIconName != null) {
        list.add(const Expanded(child: SizedBox()));
        list.add(T3Image.t3assets(T3AssetsNameString(item.detailIconName!)));
      }
      return Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: list,
      );
    }
  }

  double _viewHeight() {
    if (items.isEmpty) {
      return 0;
    }
    return items.length == 1 ? 44 : 54;
  }
}
